package com.back.demo.controller;

import com.back.demo.common.CommonResult;
import com.back.demo.controller.myVO.JudgeVO.*;
import com.back.demo.common.StudentInfo;
import com.back.demo.controller.myVO.StudentVO.StudentPositionVO;
import com.back.demo.controller.myVO.StudentVO.StudentPracticeVO;
import com.back.demo.controller.myVO.StudentVO.StudentResearchVO;
import com.back.demo.service.JudgeService;
import com.back.demo.service.StudentService;
import com.back.demo.util.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@Api(tags = "评审功能")
@RestController
@RequestMapping("/back/judge")
public class JudgeController {

    @Autowired
    private JudgeService judgeService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private StudentService studentService;


    @ApiOperation("按类型提供所有学生得分")
    @PostMapping("/all-students-single-score")
    public CommonResult<?> acquireAllStudentsSingleGrade(@RequestHeader("Authorization") String authHeader, @RequestBody AllStudentsSingleGradeReqVO allStudentsSingleGradeReqVO) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token，后续用于权限确认
        String token = authHeader.substring(7);
        String judgeId = jwtTokenUtil.getUsernameFromToken(token);

        // 查找所有学生的成绩（按照传入的类型查找）
        ArrayList<AllStudentsSingleGradeResVO> resVO = judgeService.acquireAllStudentsSingleGrade(allStudentsSingleGradeReqVO.getType());
        return CommonResult.success(resVO);
    }

    @ApiOperation("单个学生所有得分")
    @PostMapping("/single-student-all-scores")
    public CommonResult<?> acquireSingleStudentAllGrades(@RequestHeader("Authorization") String authHeader, @RequestBody SingleStudentAllGradesReqVO reqVO) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token，后续用于权限确认
        String token = authHeader.substring(7);
        String judgeId = jwtTokenUtil.getUsernameFromToken(token);

        //返回单个学生所有得分
        String studentId = reqVO.getStudentId();
        SingleStudentAllGradesResVO resVO = judgeService.acquireSingleStudentAllGrades(studentId);
        return CommonResult.success(resVO);
    }


    @ApiOperation("按学号查询学生项目")
    @PostMapping("/acquire-single-student-by-item")
    @CrossOrigin // 不明白为什么出现了跨域的问题
    public CommonResult<?> acquireSingleItem(@RequestHeader("Authorization") String authHeader, @RequestBody SingleItemReqVO reqVO) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token，后续用于权限确认
        String token = authHeader.substring(7);
        String judgeId = jwtTokenUtil.getUsernameFromToken(token);

        // 这里不太清楚如何使用一句SQL语句完成，所以分2次查询
        Integer item = reqVO.getItem();
        String studentId = reqVO.getStudentId();
        StudentInfo studentInfo = studentService.findStudentInfoByStudentId(studentId);
        String studentName = studentInfo.getStudentName();
        int userId = studentInfo.getUserId();

        switch (item) {
            case 1: // 个人学年总结
                SingleItemResVO<String> singleItemResVO1 = judgeService.acquireSingleStudentSummary(studentId, studentName, userId);
                return CommonResult.success(singleItemResVO1);
            case 2: // 科研
                SingleItemResVO<StudentResearchVO> singleItemResVO2 = judgeService.acquireSingleStudentResearch(studentId, studentName, userId);
                return CommonResult.success(singleItemResVO2);
            case 3:
                SingleItemResVO<StudentPositionVO> singleItemResVO3 = judgeService.acquireSingleStudentPosition(studentId, studentName, userId);
                return CommonResult.success(singleItemResVO3);
            case 4:
                SingleItemResVO<StudentPracticeVO> singleItemResVO4 = judgeService.acquireSingleStudentPractice(studentId, studentName, userId);
                return CommonResult.success(singleItemResVO4);
            default:
                return null;
        }
    }

    @ApiOperation("向数据库插入评分")
    @PostMapping("/rate")
    @CrossOrigin // 不明白为什么出现了跨域的问题
    public CommonResult<?> rate(@RequestHeader("Authorization") String authHeader, @RequestBody RateReqVO reqVO) {

        // 解析Authorization请求头中的JWT令牌 Bearer access_token，后续用于权限确认
        String token = authHeader.substring(7);
        String judgeId = jwtTokenUtil.getUsernameFromToken(token);

        String studentId = reqVO.getStudentId();
        try {
            //System.out.println(reqVO.getGrade());
            // 执行插入评分的操作
            judgeService.rate(studentId, reqVO.getType(), reqVO.getGrade());
        } catch (Exception e) {
            // 处理修改失败的情况
            System.out.println(e.toString());
            return CommonResult.error(50003, "打分失败");
        }
        return CommonResult.success("打分成功");
    }

}
